const fs = require("fs");

module.exports = async function data_Visible(body) {
    let dyn = body.dynasty
    let mus = body.museum
    let mus1 = JSON.stringify(mus)
    let dyn1 = JSON.stringify(dyn)
    let dyn2 = []
    let mus2 = []
    if(dyn1 === '[""]'){
        dyn2 = null
    }else{
        for(let i = 0, k = 0 ; 2*i+1 < dyn1.split('"').length; i++, k++){
            dyn2[k] = dyn1.split('"')[2*i+1]
        }

    }
    if(mus1 === '[""]'){
        mus2 = null
    }else{
        for(let i = 0, k = 0 ; 2*i+1 < mus1.split('"').length; i++, k++){
            mus2[k] = mus1.split('"')[2*i+1]

        }
    }
    // console.log(mus)
    // console.log(dyn)
    // console.log(mus1)
    // console.log(dyn1)
    // console.log(mus2)
    // console.log(dyn2)

    let MUS_JSON_DATA
    let DYN_JSON_DATA

    function readMusFileFun() {
        let dataObj;
        return new Promise(function (resolve, reject) {
            fs.readFile('./MuseumData.json', function (err, data) {
                if (err) {
                    reject(err);
                }
                dataObj = JSON.parse(data.toString());
                resolve(dataObj);
            });
        })
    }

    await readMusFileFun().then(function (dataObj) {
        MUS_JSON_DATA = dataObj//成功返回
    }).catch(function (err) {
        console.log(err);
    })

    function readDynFileFun() {
        let dataObj;
        return new Promise(function (resolve, reject) {
            fs.readFile('./DynastyData.json', function (err, data) {
                if (err) {
                    reject(err);
                }
                dataObj = JSON.parse(data.toString());
                resolve(dataObj);
            });
        })
    }

    await readDynFileFun().then(function (dataObj) {
        DYN_JSON_DATA = dataObj//成功返回
    }).catch(function (err) {
        console.log(err);
    })

    const _objToStrMap = function (obj) {
        let strMap = new Map();
        for (let k of Object.keys(obj)) {
            strMap.set(k, obj[k]);
        }
        return strMap;
    }

    const MuseumMap = _objToStrMap(MUS_JSON_DATA)
    const DynastyMap = _objToStrMap(DYN_JSON_DATA)

    if (mus2 != null) {
        for(let i = 0; i < mus2.length; i++){
            let value = MuseumMap.get(mus2[i])
            MuseumMap.set(mus2[i], value + 1)
        }
    }

    if(dyn2 != null){
        for(let i = 0; i < dyn2.length; i++){
            let value = DynastyMap.get(dyn2[i])
            DynastyMap.set(dyn2[i], value + 1)
        }
    }

    const _strMapToObj = function (strMap) {
        let obj = Object.create(null);
        for (let [k, v] of strMap) {
            obj[k] = v;
        }
        return obj;
    }

    let MuseumJson = JSON.stringify(_strMapToObj(MuseumMap))
    let DynastyJson = JSON.stringify(_strMapToObj(DynastyMap))


    fs.writeFile('./MuseumData.json', MuseumJson, function (err) {
        if (err) throw err
    })

    fs.writeFile('./DynastyData.json', DynastyJson, function (err) {
        if (err) throw err
    })
    return [MuseumJson,DynastyJson]
}